// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: DEZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 40 : TYPE_DEZSETUP

private alias object.value0  : object.aniTileTImer // Bit obtuse of a name, but it works
private alias object.value14 : object.palTimer
private alias object.value15 : object.palSlot175
private alias object.value16 : object.palSlot191
private alias object.value17 : object.palSlot188
private alias object.value18 : object.palSlot189
private alias object.value19 : object.palSlot190

// Tables

public table DEZSetup_palTable1
	0xE00000, 0xC00000, 0xA00000, 0x800000, 0x600000, 0x400000, 0x200000, 0x400000, 0x600000, 0x800000, 0xA00000, 0xC00000, 0xE00000, 0xE02000, 0xE04000, 0xE02000
end table

public table DEZSetup_palTable2
	0x00E000, 0x20C000, 0x40A000, 0x608000, 0x806000, 0xA04000, 0xC02000, 0xE00000, 0xC00020, 0xA00040, 0x800060, 0x600080, 0x4000A0, 0x2000C0, 0x0000E0, 0x0020C0
	0x0040A0, 0x006080, 0x008060, 0x00A040, 0x00C020
end table

public table DEZSetup_palTable3
	0x0040E0, 0x0000C0, 0x0000C0, 0x0060E0, 0x0020C0, 0x0000C0, 0x0040E0, 0x0040E0, 0x0000C0, 0x0020C0, 0x0060E0, 0x0020C0, 0x0000C0, 0x0040E0, 0x0040C0, 0x0000C0
	0x0020C0, 0x0040E0, 0x0000C0, 0x0000C0, 0x0060E0, 0x0020C0, 0x0000C0, 0x0040E0, 0x0020E0, 0x0000C0, 0x0020C0
end table

// Unused..?
// It's a long list of colors likely meant for a palette cycle but it doesn't match any other palette cycles
// Strangely, this table can also be found in CPZ Setup
private table DEZSetup_Unused
	0x00E000, 0x00A000, 0x006000, 0x002000, 0x000000, 0x000020, 0x000060, 0x0020A0, 0x0060E0, 0x0020A0, 0x000060, 0x000020, 0x000000, 0x200000, 0x600000, 0xA00000
	0xE00000, 0xE04000, 0xE00000, 0xA00000, 0x600000, 0x200000, 0x000000, 0x202000, 0x606000, 0xA0A000, 0xE0E000, 0xA0A000, 0x606000, 0x202000, 0x000000, 0x002000
	0x006000, 0x00A000
end table

event ObjectMain

	// Animate the converyer belts in the background of the Mecha Sonic fight
	object.aniTileTImer++
	if object.aniTileTImer == 5
		object.aniTileTImer = 0
		object.frame++
		object.frame &= 7
		temp0 = object.frame
		temp0 += 768
		Copy16x16Tile(767, temp0)
	end if

	// Manage the various cycling palettes throughout the stage
	object.palTimer++
	if object.palTimer == 8
		object.palTimer = 0

		object.palSlot175++
		object.palSlot175 &= 15
		GetTableValue(temp0, object.palSlot175, DEZSetup_palTable1)
		SetPaletteEntry(0, 175, temp0)

		object.palSlot191++
		object.palSlot191 %= 21
		GetTableValue(temp0, object.palSlot191, DEZSetup_palTable2)
		SetPaletteEntry(0, 191, temp0)

		object.palSlot188 += 3
		object.palSlot188 %= 27
		GetTableValue(temp0, object.palSlot188, DEZSetup_palTable3)
		SetPaletteEntry(0, 188, temp0)

		object.palSlot189 += 3
		object.palSlot189 %= 27
		GetTableValue(temp0, object.palSlot189, DEZSetup_palTable3)
		SetPaletteEntry(0, 189, temp0)

		object.palSlot190 += 3
		object.palSlot190 %= 27
		GetTableValue(temp0, object.palSlot190, DEZSetup_palTable3)
		SetPaletteEntry(0, 190, temp0)
	end if
end event


event ObjectStartup
	// Death Egg music, loops from the begining
	SetMusicTrack("DeathEgg.ogg", 0, true)

	SetPaletteEntry(0, 192, 0) // Set color index 192 to be black

	Copy16x16Tile(767, 768)

	// There shouldn't be any animals in DEZ
	animalType1 = TypeName[Blank Object]
	animalType2 = TypeName[Blank Object]

	// Remove player 2
	//object[1].type = TypeName[Blank Object]
	//playerCount = 1

	// Make object 10 a DEZ Setup object
	object[10].type = TypeName[DEZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	object[10].palSlot189 = 1
	object[10].palSlot190 = 2

	// Set stage bounds
	temp0 = stage.curYBoundary2
	temp0 -= screen.ysize
	stage.newYBoundary1 = temp0
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
